home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / SCHMOO.ZIP / INIT.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  12KB  |  457 lines

  1. /*
  2.  * INIT.C
  3.  *
  4.  * Application (not OLE) specific initialization code.
  5.  *  FApplicationInit
  6.  *  FFileInit
  7.  *  HLoadAppStrings
  8.  *  HListParse
  9.  *  PszWhiteSpaceScan
  10.  *
  11.  * FApplicationInit makes some calls into OLEINST.C and OLEINIT.C
  12.  *
  13.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  14.  *
  15.  */
  16.  
  17. #include <windows.h>
  18. #include <ole.h>
  19. #include "schmoo.h"
  20. #include "oleinst.h"
  21. #include "oleglobl.h"
  22.  
  23.  
  24.  
  25. /*
  26.  * FApplicationInit
  27.  *
  28.  * Purpose:
  29.  *  All application specific initialization including loading
  30.  *  the stringtable, registering window classes, and calling any
  31.  *  OLE specific initialziation code.
  32.  *
  33.  * Parameters:
  34.  *  pGlob           LPGLOBALS to global variable block.
  35.  *  hPrevInstance   HANDLE to the previous application instance, if any.
  36.  *
  37.  * Return Value:
  38.  *  BOOL            TRUE if everything succeeds, FALSE otherwise.
  39.  *                  If FALSE is returned, allocated memory and objects
  40.  *                  are not necessarily freed.  The caller should then
  41.  *                  use the FApplicationExit function to perform cleanup.
  42.  */
  43.  
  44. BOOL FAR PASCAL FApplicationInit(LPGLOBALS pGlob, HANDLE hPrevInstance)
  45.     {
  46.     HANDLE      hMem;
  47.     LPSTR FAR * ppszCmds;
  48.     LPSTR FAR * ppszT;
  49.     BOOL        fRet=TRUE;
  50.  
  51.  
  52.     /*
  53.      * InitApp allocates local memory for strings. WinMain must free.
  54.      * If this fails, we should quit BEFORE we register any classes
  55.      * or do anything else to suck up USER or GDI resources.
  56.      */
  57.     hMem=HLoadAppStrings(pGlob);
  58.  
  59.     if (NULL==hMem)
  60.         return FALSE;
  61.  
  62.     pGlob->hStringMem=hMem;
  63.  
  64.     //Classes are only registered if hPrevInstance is NULL.
  65.     if (!FClassRegister(pGlob, hPrevInstance))
  66.         {
  67.         LocalFree(pGlob->hStringMem);
  68.         return FALSE;
  69.         }
  70.  
  71.     //Register a private clipboard format, same as the class name.
  72.     pGlob->cfSchmoo=RegisterClipboardFormat(rgpsz[IDS_CLASSSCHMOO]);
  73.  
  74.     hMem=HListParse(pGlob->pszCmdLine);
  75.     ppszCmds=(LPSTR FAR *)(PSTR)hMem;
  76.     ppszT=ppszCmds;
  77.  
  78.     /*
  79.      * Scan the command line list for the first thing without a
  80.      * / or - which is out initial file.
  81.      */
  82.  
  83.     while (*ppszT)
  84.         {
  85.         if ('-'!=**ppszT && '/'!=**ppszT)
  86.             break;
  87.  
  88.         ppszT++;
  89.         }
  90.  
  91.     //Copy this filename for later loading during WM_CREATE.
  92.     if (NULL==*ppszT)
  93.         pGlob->szFile[0]=0;
  94.     else
  95.         lstrcpy(pGlob->szFile, *ppszT);
  96.  
  97.  
  98. #ifdef MAKEOLESERVER
  99.     /*
  100.      * Go do anything that deals with the registration database.
  101.      * Installation could be moved to an installation program
  102.      */
  103.     if (!FRegDBInstall())
  104.         {
  105.         LocalFree(hMem);
  106.         return FALSE;
  107.         }
  108.  
  109.     //Initialize OLE specific data.  FOLEInstanceInit affects pGlob->fOLE
  110.     if (FOLEInstanceInit(pOLE, pGlob->hInst, rgpsz[IDS_CLASSSCHMOO],
  111.                          ppszCmds, pGlob->nCmdShow))
  112.         {
  113.         //We will open any linked file later in WM_CREATE.
  114.  
  115.         //Copy the new ShowWindow parameter.
  116.         pGlob->nCmdShow=pOLE->pSvr->wCmdShow;
  117.         }
  118.     else
  119.         fRet=FALSE;
  120.  
  121. #endif //MAKEOLESERVER
  122.  
  123.     //Free the command-line string list in hMem.
  124.     LocalFree(hMem);
  125.     return fRet;
  126.     }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. /*
  135.  * FClassRegister
  136.  *
  137.  * Purpose:
  138.  *  Registers classes used by the application:  "Schmoo" the main
  139.  *  window, and "Polyline" the editing window.
  140.  *
  141.  * Parameters:
  142.  *  pGlob           LPGLOBALS to the global variables block.
  143.  *  hPrevInstance   HANDLE of any previous application instance.
  144.  *
  145.  * Return Value:
  146.  *  BOOL            TRUE if all classes are successfully registered
  147.  *                  (or if hPrevInstance is non-NULL).  FALSE is
  148.  *                  any registration fails.
  149.  *
  150.  */
  151.  
  152. BOOL FAR PASCAL FClassRegister(LPGLOBALS pGlob, HANDLE hPrevInstance)
  153.     {
  154.     WNDCLASS        wc;
  155.  
  156.     if (hPrevInstance)
  157.         return TRUE;
  158.  
  159.     /*
  160.      * Note that we do not need to unregister classes on a failure
  161.      * since that's part of automatic app cleanup.
  162.      */
  163.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  164.     wc.lpfnWndProc   = SchmooWndProc;
  165.     wc.cbClsExtra    = 0;
  166.     wc.cbWndExtra    = 0;
  167.     wc.hInstance     = pGlob->hInst;
  168.     wc.hIcon         = LoadIcon(pGlob->hInst, "Icon");
  169.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  170.     wc.hbrBackground = COLOR_APPWORKSPACE + 1;
  171.     wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
  172.     wc.lpszClassName = rgpsz[IDS_CLASSSCHMOO];
  173.  
  174.     if (!RegisterClass(&wc))
  175.         return FALSE;
  176.  
  177.  
  178.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  179.     wc.lpfnWndProc   = PolylineWndProc;
  180.     wc.cbClsExtra    = 0;
  181.     wc.cbWndExtra    = CBPOLYLINEWNDEXTRA;
  182.     wc.hInstance     = pGlob->hInst;
  183.     wc.hIcon         = NULL;
  184.     wc.hCursor       = LoadCursor(NULL, IDC_CROSS);
  185.     wc.hbrBackground = COLOR_WINDOW + 1;
  186.     wc.lpszMenuName  = NULL;
  187.     wc.lpszClassName = rgpsz[IDS_CLASSPOLYLINE];
  188.  
  189.     if (!RegisterClass(&wc))
  190.         return FALSE;
  191.  
  192.     return TRUE;
  193.     }
  194.  
  195.  
  196.  
  197.  
  198.  
  199. /*
  200.  * FFileInit
  201.  *
  202.  * Purpose:
  203.  *  Loads a file specified on the command line and sets the polyline
  204.  *  window data to reflect the contents of that file.  This is set
  205.  *  in a different function instead of in FApplicationInit since
  206.  *  it depends on the Polyline window being created which has not
  207.  *  happened by FApplicationInit time.
  208.  *
  209.  * Parameters:
  210.  *  pGlob           LPGLOBALS to global variable block.
  211.  *
  212.  * Return Value:
  213.  *  BOOL            TRUE if everything succeeded, FALSE otherwise.
  214.  *
  215.  */
  216.  
  217. BOOL FAR PASCAL FFileInit(LPGLOBALS pGlob)
  218.     {
  219.     POLYLINE        pl;
  220.  
  221.     /*
  222.      * If there is a file to load, then load it and change the
  223.      * window title.  If loading fails, then default to a new
  224.      * untitled document.
  225.      */
  226.     if (0!=pGlob->szFile[0])
  227.         {
  228.         if (!FMooFileRead(pGlob, pGlob->szFile, &pl))
  229.             {
  230.             //If we're in OLE linking mode, return unsuccessful
  231.             if (pGlob->fOLE)
  232.                 return FALSE;
  233.  
  234.             pGlob->szFile[0]=0;
  235.             WindowTitleSet(pGlob->hWnd, rgpsz[IDS_UNTITLED]);
  236.             pGlob->fOpenFile=FALSE;
  237.  
  238.             }
  239.         else
  240.             {
  241.             SendMessage(pGlob->hWndPolyline, PLM_POLYLINESET, TRUE,
  242.                         (LONG)(LPSTR)&pl);
  243.             WindowTitleSet(pGlob->hWnd, pGlob->szFile);
  244.             pGlob->fOpenFile=TRUE;
  245.             }
  246.         }
  247.  
  248.     return TRUE;
  249.     }
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257. /*
  258.  * HLoadAppStrings
  259.  *
  260.  * Purpose:
  261.  *  Allocates FIXED local memory and reads the applications
  262.  *  string resources into that memory.  Each string's pointer
  263.  *  is available with rgpsz[i] where i is the ID value of the
  264.  *  string.  The strings must have sequential IDs.
  265.  *
  266.  * Parameters:
  267.  *  pGlob           LPGLOBALS to global variable block.
  268.  *
  269.  * Return Value:
  270.  *  HANDLE          Handle to the local memory.  NULL if memory could
  271.  *                  not be allocated.
  272.  */
  273.  
  274. HANDLE FAR PASCAL HLoadAppStrings(LPGLOBALS pGlob)
  275.     {
  276.     HANDLE      hLocalMem;
  277.     char NEAR   *pch;
  278.     WORD        cchUsed=0;
  279.     WORD        cch;
  280.     short       i;
  281.  
  282.     /*
  283.      * Allocate memory and load strings.  NOTE!  The LPTR style
  284.      * specifies FIXED memory.  This should not be a big deal
  285.      * since this is an early allocation into the local heap.
  286.      * But it should be watched if the number of strings becomes
  287.      * large.
  288.      */
  289.     hLocalMem=LocalAlloc(LPTR, CSTRINGS*CCHSTRINGMAX);
  290.  
  291.     if (hLocalMem==NULL)
  292.         return (HANDLE)NULL;
  293.  
  294.     /*
  295.      * This operation is only valid for FIXED memory.  Otherwise use
  296.      * LocalLock.
  297.      */
  298.     pch=(char *)hLocalMem;
  299.  
  300.  
  301.     /*
  302.      * Load the strings into the memory and retain the specific
  303.      * pointer to that string.
  304.      */
  305.     for (i=0; i<CSTRINGS; i++)
  306.         {
  307.         cch=LoadString(pGlob->hInst, i, (LPSTR)(pch+cchUsed), CCHSTRINGMAX-1);
  308.         rgpsz[i]=(char *)(pch+cchUsed);
  309.  
  310.         /*
  311.          * One is added to cch to include a NULL.  The memory was ZEROINITed
  312.          * on allocation so by skipping a byte we get the NULL.
  313.          */
  314.         cchUsed +=cch+1;
  315.         }
  316.  
  317.     /*
  318.      * We are assuming that no string is over CCHSTRINGMAX, and therefore
  319.      * we did not use all the allocated memory.  Therefore LocalReAlloc
  320.      * will only SHRINK the block, never expand it.  So if it fails, we
  321.      * don't care--all the strings are still there, we just wasted some
  322.      * space.
  323.      */
  324.     LocalReAlloc(hLocalMem, cchUsed+1, LPTR);
  325.  
  326.     return hLocalMem;
  327.     }
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336. /*
  337.  * HListParse
  338.  *
  339.  * Purpose:
  340.  *  Parses any string containing text separated by whitespace into
  341.  *  a list of pointers into that string as well as overwriting the
  342.  *  whitespace with null terminators.  The result is that each
  343.  *  pointer in the list points to its own null-terminated string,
  344.  *  but those strings are not necessarily contiguous.
  345.  *
  346.  *  Since MS-DOS command lines are limited to 128 characters, this
  347.  *  function limits the number of arguments to 64 separate strings.
  348.  *
  349.  * Parameters:
  350.  *  psz             LPSTR to the string to parse.
  351.  *
  352.  * Return Value:
  353.  *  HANDLE          Local LMEM_FIXED memory handle containing the
  354.  *                  list of LPSTRs to the list items.  NULL if memory
  355.  *                  could not be allocated.
  356.  */
  357.  
  358. HANDLE FAR PASCAL HListParse(LPSTR psz)
  359.     {
  360.     HANDLE       hMem;
  361.     LPSTR FAR   *ppsz;
  362.     LPSTR        pszT;
  363.     WORD         cp;
  364.  
  365.     //Allocate space for 64 pointers.
  366.     hMem=LocalAlloc(LPTR, 64*sizeof(LPSTR));
  367.  
  368.     if (NULL==hMem)
  369.         return NULL;
  370.  
  371.     ppsz=(LPSTR FAR *)(PSTR)hMem;
  372.     cp=0;
  373.  
  374.     /*
  375.      * For each string, scan for whitespace, save that pointer in
  376.      * ppsz, and null-terminate that string.  If it was already
  377.      * null-terminated, then there are no more pieces in the list.
  378.      */
  379.  
  380.     while (0!=*psz)
  381.         {
  382.         //Skip to beginning of first item.
  383.         psz=PszWhiteSpaceScan(psz, TRUE);
  384.  
  385.         //If it's a zero, stop here.
  386.         if (0==*psz)
  387.             break;
  388.  
  389.         //Find the end of this item.
  390.         pszT=PszWhiteSpaceScan(psz, FALSE);
  391.  
  392.         //Null terminate this string and point to next character.
  393.         if (0!=*pszT)
  394.             *pszT++=0;
  395.  
  396.         //Save this string pointer.
  397.         *ppsz++=psz;
  398.         cp++;
  399.  
  400.         //Check our limit of 64.
  401.         if (64 <= cp)
  402.             break;
  403.  
  404.         //Check next item.
  405.         psz=pszT;
  406.         }
  407.  
  408.     return hMem;
  409.     }
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416. /*
  417.  * PszWhiteSpaceScan
  418.  *
  419.  * Purpose:
  420.  *  Skips characters in a string until a whitespace or non-whitespace
  421.  *  character is seen.  Whitespace is defined as \n, \r, \t, or ' '.
  422.  *
  423.  * NOTE:  This function is not extremely well suited to localization.
  424.  *        It assumes that an existing application seeking to become
  425.  *        and OLE server probably already has such a string function
  426.  *        available.
  427.  *
  428.  * Parameters:
  429.  *  psz             LPSTR to string to manipulate
  430.  *  fSkip           BOOL  TRUE if we want to skip whitespace.
  431.  *                  FALSE if we want to skip anything but whitespace.
  432.  *
  433.  * Return Value:
  434.  *  LPSTR           Pointer to first character in the string that either
  435.  *                  non-whitespace (fSkip=TRUE) or whitespace (fSkip=FALSE),
  436.  *                  which may be the null terminator.
  437.  */
  438.  
  439. LPSTR PASCAL PszWhiteSpaceScan(LPSTR psz, BOOL fSkip)
  440.     {
  441.     char        ch;
  442.     BOOL        fWhite;
  443.  
  444.     while (ch=*psz)
  445.         {
  446.         fWhite=('\n'==ch || '\r'==ch || '\t'==ch || ' '==ch);
  447.  
  448.         //Too bad C doesn't have a logical XOR (^^) operator.
  449.         if ((fSkip && !fWhite) || (!fSkip && fWhite))
  450.             break;
  451.  
  452.         psz++;
  453.         }
  454.  
  455.     return psz;
  456.     }
  457.